﻿// -------------------------------------------------------------------------
//    @FileName         :    NFMapModule.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-10
//    @Email			:    445267987@qq.com
//    @Module           :    NFMapModule
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFMapModule.h"

#include "NFMapPart.h"
#include "NFMapUtil.h"
#include "NFPacketSendUtil.h"
#include "NFPlayer.h"
#include "NFPlayerUtil.h"
#include "NFServiceSns.h"
#include "NFSession.h"
#include "proto_svr_common.nanopb.h"

NFMapModule::NFMapModule(NFIPluginManager* p) : NFLogicDynamicModule(p)
{
}

NFMapModule::~NFMapModule()
{
}

int NFMapModule::Awake()
{
    RegisterClientMessage(NF_ST_LOGIC_SERVER, E_CS_MAP_QUERY_REQ);
    RegisterServerMessage(NF_ST_LOGIC_SERVER, E_GC_CITYSVR_MAPDATA_NTF);
    return 0;
}

int NFMapModule::OnHandleClientMessage(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case E_CS_MAP_QUERY_REQ:
        {
            iRetCode = OnHandleMapQueryReq(packet, pSession, req);
            break;
        }
        default: NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
    }

    return iRetCode;
}

int NFMapModule::OnHandleServerMessage(uint32_t msgId, NFDataPackage& packet, uint64_t param1, uint64_t param2)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case E_GC_CITYSVR_MAPDATA_NTF:
        {
            iRetCode = OnHandleMapDataNtf(msgId, packet, param1, param2);
            break;
        }
        default: NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
    }

    return iRetCode;
}

int NFMapModule::OnHandleMapQueryReq(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    int iRetCode = 0;
    CHECK_NULL(0, pSession);

    NFPlayer* pPlayer = pSession->GetPlayer();
    CHECK_NULL(0, pPlayer);

    if(req.map_query_req().use_point())
    {
        LOG_ERR(pPlayer->GetRoleID(), -1, "invalid param about position:{}", pPlayer->GetRoleID());
        return -1;
    }

    if (pPlayer->IsInBattle())
    {
        iRetCode = proto_ff::ERR_CODE_TRANS_IN_BATTLE;
        LOG_ERR(pPlayer->GetRoleID(), iRetCode, "player is in battle !");
        return iRetCode;
    }

    iRetCode = OnHandlePlayerTransReq(pPlayer, req);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "OnHandlePlayerTransReq failed, ret:{}", iRetCode);

    if (iRetCode != 0)
    {
        return NFMapUtil::StartEnterDefaultCity(pPlayer);
    }
    return 0;
}

int NFMapModule::OnHandlePlayerTransReq(NFPlayer* pPlayer, const proto_ff::Proto_CSReq& req)
{
    int iRetCode = 0;
    CHECK_NULL(0, pPlayer);
    NFMapPart* pMapPart = pPlayer->GetPart<NFMapPart>();
    CHECK_NULL(0, pMapPart);

    const proto_ff::Proto_CSMapQueryReq& oCSMapQueryReq = req.map_query_req();
    if (pMapPart->IsEnterCity())
    {
        iRetCode = NFMapUtil::CheckEnterMap(pPlayer, oCSMapQueryReq.map_desc_id());
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "CheckEnterMap failed, ret:{}", iRetCode);
    }

    iRetCode = NFMapUtil::StartEnterMap(pPlayer, req);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "StartEnterMap failed, ret:{}", iRetCode);

    return 0;
}

int NFMapModule::OnHandleMapDataNtf(uint32_t msgId, NFDataPackage& packet, uint64_t param1, uint64_t param2)
{
    proto_ff::Proto_GCMapDataNtf ntf;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, ntf);
    uint64_t ullRoleID = ntf.role_id();
    NFPlayer *pPlayer = NFPlayerUtil::GetPlayerByRoleID( ullRoleID );
    if( pPlayer )
    {
        NFMapPart* pMapPart = pPlayer->GetPart<NFMapPart>();
        CHECK_NULL(0, pMapPart);
        pMapPart->SynMapData(&ntf.map_data());
    }

    return 0;
}
